package org.itheima.controller;


import org.itheima.pojo.UserSearchHistory;
import org.itheima.service.CompareService;
import org.itheima.service.RecommendationService;
import org.itheima.service.UserSearchHistoryService;
import org.itheima.service.WarehouseService;
import org.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {


    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private RecommendationService recommendationService;


    @Autowired
    private CompareService compareService;


    @GetMapping("/title")
    public List<Map<String, Object>> CommodityInformation(String title) {
        List<Map<String, Object>> warehouse = warehouseService.findByTitle(title);
       /* System.out.println(warehouse.get(0));*/
        return warehouse;
    }

    @GetMapping("/numberOfComments")
    public List<Map<String, Object>> HotProducts(String numberOfComments){
        List<Map<String, Object>> warehouse = warehouseService.HotItems(numberOfComments);
        /* System.out.println(warehouse.get(0));*/
        return warehouse;
    }

    @PostMapping("/search")
    public List<Map<String, Object>> search(@RequestBody Map<String, String> request) {
        String query = request.get("query"); // 获取前端传来的 "query" 字段
        System.out.println("前端传来的搜索值: " + query);
        List<Map<String, Object>> response = warehouseService.findByTitle(query);
        Map<String,Object> map = ThreadLocalUtil.get();
        int user_id = (int) map.get("id");
        warehouseService.chaRuSouSuoJiLu(user_id,query);
        return response;
    }

    @GetMapping("/all")
    public  List<Map<String,Object>> findAll(){
        List<Map<String,Object>> warehouse =  warehouseService.findAll();
        return warehouse;
    }

    @GetMapping("/recommend")
    public List<Map> recommend(){
        Map<String,Object> map = ThreadLocalUtil.get();
        int user_id = (int) map.get("id");
        String username = (String) map.get("username");
        List<Map> xiaoguo =  recommendationService.getRecommendedProducts(user_id,username);
        System.out.println(xiaoguo);
        return xiaoguo;
    }

    @GetMapping("/compare")
    public List<Map<String, Object>> compare(@RequestParam("name") String name,@RequestParam("table") Integer table) {
        System.out.println("前端name是:"+name);
        System.out.println("前端传来的table是："+table);
        List<Map.Entry<Map<String, Object>, Double>> scoredList = new ArrayList<>();
        String firstTwo = name.length() >= 2 ? name.substring(0, 2) : name;
        List<Map<String, Object>> warehouse1 = new ArrayList<>();
        List<Map<String, Object>> warehouse2;
        if(table==1){
        warehouse1 = warehouseService.jingdongdiannaoshuju(firstTwo);
        warehouse2 = warehouseService.taobaodiannaoshuju(firstTwo);
        warehouse1.addAll(warehouse2);
        }
        if(table==2){
            warehouse1 = warehouseService.jingdongdianzishoubiaoshuju(firstTwo);
            warehouse2 = warehouseService.taobaodianzishoubiaoshuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        if(table == 3){
            warehouse1 = warehouseService.jingdongerjishuju(firstTwo);
            warehouse2 = warehouseService.taobaoerjishuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        if(table ==  4){
            warehouse1 = warehouseService.jingdongjianpanshuju(firstTwo);
            warehouse2 = warehouseService.taobaojianpanshuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        if(table == 5){
            warehouse1 = warehouseService.jingdongpingbanshuju(firstTwo);
            warehouse2 = warehouseService.taobaopingbanshuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        if(table == 6){
            warehouse1 = warehouseService.jingdongshoujishuju(firstTwo);
            warehouse2 = warehouseService.taobaoshoujishuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        if(table == 7){
            warehouse1 = warehouseService.jingdongshubiaoshuju(firstTwo);
            warehouse2 = warehouseService.taobaoshubiaoshuju(firstTwo);
            warehouse1.addAll(warehouse2);
        }
        for (Map<String, Object> item : warehouse1) {
            Object nameObj = item.get("name");
            System.out.println("nameObj:"+nameObj);
            if (nameObj != null) {
                String name2 = nameObj.toString();
                double score = compareService.computeCosineSimilarity(name, name2);
                scoredList.add(new AbstractMap.SimpleEntry<>(item, score));
            }
        }
        // 根据相似度排序（从高到低）
        scoredList.sort((a, b) -> Double.compare(b.getValue(), a.getValue()));

        System.out.println("相似度前10是"+scoredList.stream()
                .limit(10)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList()));

        //提取前十条
        return scoredList.stream()
                .limit(10)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }
    


}
